package com.gopay.solution.domain.bo.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.solution.LegalObjReq;
import com.gopay.common.domain.solution.LegalObjRes;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsTransLegalObjDtl;
import com.gopay.solution.domain.vo.Event;
import com.gopay.solution.domain.vo.Result;

/**
 * 方案服务 交易合法对方方案明细 规则执行器
 * 
 * @SsTransLegalObjSoluRuleExecutor.java
 * @author MA HUAN
 * @2013-3-27 下午4:24:44 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssTransLegalObjSoluRuleExecutor")
public class SsTransLegalObjSoluRuleExecutor extends AbstractSolutionRuleExecutor {

    /*
     *只有该集合中的交易才有合法交易对方配置，其他交易的解决方案结果均置为false
     *可配置交易列表请参照产品文档《控制台需求规格说明书_系统管理_产品线管理.docx》 中交易方案-合法交易对方章节的有关说明
     */
    private static Set<String> configuredIntTxnCdSet = new HashSet<String>();
    static {
        configuredIntTxnCdSet.add(IntTxnCd._00200.value);
        configuredIntTxnCdSet.add(IntTxnCd._00201.value);
        configuredIntTxnCdSet.add(IntTxnCd._00202.value);
        configuredIntTxnCdSet.add(IntTxnCd._00207.value);
        configuredIntTxnCdSet.add(IntTxnCd._00900.value);
        configuredIntTxnCdSet.add(IntTxnCd._00901.value);
        configuredIntTxnCdSet.add(IntTxnCd._00100.value);
        configuredIntTxnCdSet.add(IntTxnCd._00400.value);
    }
    
    @Override
    public Result execute(List<SolutionRuleDtl> rules, Event event) {
        

        Serializable input = event.getDetail();

        LegalObjReq req = (LegalObjReq) input;
        IntTxnCd intTxnCd = req.getIntTxnCd();
        
        Result result = new Result();
        result.setSolved(false);
        result.setFailedReason("");
        result.setDetail(null);

        if (null == rules || rules.isEmpty() || !configuredIntTxnCdSet.contains(intTxnCd.value)) {
            result.setFailedReason("查询不到合法交易对方方案记录");
            return result;
        }

        //加载指定交易的合法交易对方集合
        Set<String> legalObjAps = new HashSet<String>();
        
        SsTransLegalObjDtl legalObjDtl = null;
        for (SolutionRuleDtl rule : rules) {
            
            legalObjDtl = (SsTransLegalObjDtl) rule;
            
            if (legalObjDtl.getGopayIntTxnCd().equals(intTxnCd.value)) {
            	legalObjAps.add(legalObjDtl.getObjAcctProdCode());
            }
        }
        
        //遍历请求上送的账户产品列表，检查其是否在合法交易对方集合中
        boolean isLegalObj = true;
        List<String> illegalObjAccts = new ArrayList<String>();
        Map<String, List<String>> tranObjAps = req.getTranObjAps();
        if(tranObjAps == null || tranObjAps.isEmpty()) {
        	isLegalObj = false;
        }
        else {
        	String objAp = null;
        	for(Iterator<String> it = tranObjAps.keySet().iterator(); it.hasNext();) {
        		objAp = it.next();
        		
        		//如果合法交易对方集合中不包含该账户产品，则返回错误
        		if(!legalObjAps.contains(objAp)) {
        			isLegalObj = false;
        			illegalObjAccts.addAll((List<String>)tranObjAps.get(objAp));
        		}
        	}
        }
        
        LegalObjRes res = new LegalObjRes();
        res.setAllowed(isLegalObj);
        res.setIllegalObjAccts(illegalObjAccts);
        List<String> retLegalObjAps = new ArrayList<String>();
        retLegalObjAps.addAll(legalObjAps);
        res.setLegalObjAcctProds(retLegalObjAps);

        result.setSolved(true);
        result.setFailedReason("");
        result.setDetail(res);

        

        return result;
    }

}
